package com.wt.struggle.goodbye.main.fragment

import android.content.Intent
import android.os.Bundle
import android.support.design.widget.TabLayout
import android.support.v4.app.FragmentPagerAdapter
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.*
import com.wt.struggle.goodbye.info.NetInfo
import com.wt.struggle.goodbye.main.activity.*
import kotlinx.android.synthetic.main.main_page_fragment_layout.*

class MainPageFragment : ProV4Fragment() {

    override fun handler(what: Int, arg1: Int, netInfo: NetInfo) {

    }

    override fun createView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return inflater.inflate(R.layout.main_page_fragment_layout, container, false)

    }


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initTabLayout()
        initClick()

    }

    override fun onResume() {
        super.onResume()

    }


    val tabLayoutList = arrayListOf<String>("关注", "发现", "附近")

    val fragmentList = arrayListOf<ProV4Fragment>(MainFragment1(), MainFragment2(), MainFragment3())

    private fun initTabLayout() {

        for (i in 0 until tabLayoutList.size) {
            val tab = tabLayout.newTab()
            tabLayout.addTab(tab.setText(tabLayoutList[i]), false)

        }

        tabLayout.getTabAt(1)!!.select()

        val ww=context!!.resources.getDimension(R.dimen.dp_8).toInt()

        tabLayout.post {
            setIndicator(tabLayout, ww, ww)
        }

        contentViewPager.adapter = object : FragmentPagerAdapter(childFragmentManager) {
            override fun getItem(position: Int) = fragmentList[position]

            override fun getCount() = fragmentList.size

            override fun getPageTitle(position: Int): CharSequence? {
                return tabLayoutList[position]
            }

        }

        tabLayout.setupWithViewPager(contentViewPager)

        tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabSelected(tab: TabLayout.Tab?) {

                val position = tab!!.position
                contentViewPager.setCurrentItem(position, true)

                Log.i("result", "result------$position")
            }
        })

        contentViewPager.setCurrentItem(1, true)

        contentViewPager.addOnPageChangeListener(object : TabLayout.TabLayoutOnPageChangeListener(tabLayout) {})

    }


    /**
     * 循环改变样式
     */
    fun change(tabLayoutList: ArrayList<String>, position: Int) {
        for (i in tabLayoutList.indices) {
            val tab = tabLayout.getTabAt(i)
            if (tab != null) {
                val tttView: View = tab.customView!!
                if (tttView != null) {
                    val textView = tttView.findViewById<TextView>(R.id.tablayoutTextView)
                    changeTextStyle(textView, i == position)
                }
            }
        }
    }


    private fun initClick() {
        searchLinear.setOnClickListener {
            startActivity(Intent(activity, SearchActivity::class.java))
        }

    }

}